package org.easy.common.restinfo;

import java.io.Serializable;

public class RestInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private final boolean success;
    private final String msg;
    private final Integer code;
    private final Integer count;
    private final T data;

    public RestInfo(boolean success, String message, Integer code, int count, T result) {
        this.success = success;
        this.msg = message;
        this.code = code;
        this.count = count;
        this.data = result;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public String getMsg() {
        return this.msg;
    }

    public Integer getCode() {
        return this.code;
    }

    public Integer getCount() {
        return this.count;
    }

    public T getData() {
        return this.data;
    }

    public String toString() {
        return "RestInfo{success=" + this.success + ", message='" + this.msg + '\'' + ", code=" + this.code + ", count=" + this.count + ", result=" + this.data + '}';
    }

    public static RestInfo.RestInfoBuilder ok() {
        return success().code(0);
    }

    public static <T> RestInfo<T> ok(T result) {
        return ok().result(result);
    }

    public static RestInfo.RestInfoBuilder error() {
        return fail().code(500);
    }

    public static RestInfo.RestInfoBuilder success() {
        return fail().success(true);
    }

    public static RestInfo.RestInfoBuilder fail() {
        return new RestInfo.RestInfoBuilder();
    }

    public static final class RestInfoBuilder {
        private boolean success;
        private String msg;
        private Integer code;
        private Integer count;

        private RestInfoBuilder() {
        }

        public RestInfo.RestInfoBuilder success(boolean success) {
            this.success = success;
            return this;
        }

        public RestInfo.RestInfoBuilder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public RestInfo.RestInfoBuilder code(Integer code) {
            this.code = code;
            return this;
        }

        public RestInfo.RestInfoBuilder count(Integer count) {
            this.count = count;
            return this;
        }

        public <T> RestInfo<T> build() {
            return this.result(null);
        }

        public <T> RestInfo<T> result(T result) {
            return new RestInfo(this.success, this.msg, this.code, 0, result);
        }
    }
}